package com.ruoyi.statistics.service.impl;

import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.labdata.domain.LabOrgnization;
import com.ruoyi.labdata.service.ILabOrgnizationService;
import com.ruoyi.labdata.service.ILabOrgnizationThirdService;
import com.ruoyi.statistics.domain.LabStatisticsBaseCollege;
import com.ruoyi.statistics.mapper.LabStatisticsBaseCollegeMapper;
import com.ruoyi.statistics.service.ILabStatisticsBaseCollegeService;
import com.ruoyi.statistics.service.ILabStatisticsEquipmentService;
import com.ruoyi.statistics.service.ILabStatisticsLaboratoryService;
import com.ruoyi.statistics.service.ILabStatisticsPersonService;
import com.ruoyi.statistics.service.ILabStatisticsProjectService;
import com.ruoyi.statistics.utils.StatisticsTypeEnum;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学院高基Service业务层处理
 * 
 * @author jun.wang
 * @date 2022-05-12
 */
@Service
public class LabStatisticsBaseCollegeServiceImpl implements ILabStatisticsBaseCollegeService 
{
    @Autowired
    private LabStatisticsBaseCollegeMapper labStatisticsBaseCollegeMapper;

    @Autowired
    private ILabStatisticsPersonService personService;
    @Autowired
    private ILabStatisticsEquipmentService equipmentService;
    @Autowired
    private ILabStatisticsLaboratoryService laboratoryService;
    @Autowired
    private ILabStatisticsProjectService projectService;

    @Autowired
    private ILabOrgnizationService orgnizationService;

    /**
     * 查询学院高基
     * 
     * @param id 学院高基ID
     * @return 学院高基
     */
    @Override
    public LabStatisticsBaseCollege selectLabStatisticsBaseCollegeById(Long id)
    {
        return labStatisticsBaseCollegeMapper.selectLabStatisticsBaseCollegeById(id);
    }

    /**
     * 查询学院高基列表
     * 
     * @param labStatisticsBaseCollege 学院高基
     * @return 学院高基
     */
    @Override
    public List<LabStatisticsBaseCollege> selectLabStatisticsBaseCollegeList(LabStatisticsBaseCollege labStatisticsBaseCollege)
    {
        return labStatisticsBaseCollegeMapper.selectLabStatisticsBaseCollegeList(labStatisticsBaseCollege);
    }

    /**
     * 新增学院高基
     * 
     * @param labStatisticsBaseCollege 学院高基
     * @return 结果
     */
    @Override
    @Transactional
    public int insertLabStatisticsBaseCollege(LabStatisticsBaseCollege labStatisticsBaseCollege)
    {
        Long[] collegeOrgIdArray = labStatisticsBaseCollege.getCollegeOrgIdArray();
        for (Long collegeOrgId : collegeOrgIdArray) {
            LabStatisticsBaseCollege baseCollege = new LabStatisticsBaseCollege();
            baseCollege.setBaseId(labStatisticsBaseCollege.getBaseId());
            baseCollege.setCheckStatus("0");
            baseCollege.setType(labStatisticsBaseCollege.getType());
            baseCollege.setCollegeOrgId(collegeOrgId);
            baseCollege.setCreateTime(DateUtils.getNowDate());
            baseCollege.setCreateBy(SecurityUtils.getUsername());
            baseCollege.setUpdateTime(DateUtils.getNowDate());

            saveOrUpdateBaseCollege(baseCollege);
        }
        return collegeOrgIdArray.length;
    }

    private void saveOrUpdateBaseCollege(LabStatisticsBaseCollege baseCollege) {
        LabStatisticsBaseCollege query = new LabStatisticsBaseCollege();
        query.setBaseId(baseCollege.getBaseId());
        query.setType(baseCollege.getType());
        query.setCollegeOrgId(baseCollege.getCollegeOrgId());
        List<LabStatisticsBaseCollege> list = labStatisticsBaseCollegeMapper.selectLabStatisticsBaseCollegeList(
            query);
        if (CollectionUtils.isEmpty(list)) {
            baseCollege.setUpdateTime(DateUtils.getNowDate());
            labStatisticsBaseCollegeMapper.insertLabStatisticsBaseCollege(baseCollege);
        } else {
            LabStatisticsBaseCollege oldBaseCollege = list.get(0);
            oldBaseCollege.setUpdateBy(SecurityUtils.getUsername());
            oldBaseCollege.setUpdateTime(DateUtils.getNowDate());
            labStatisticsBaseCollegeMapper.updateLabStatisticsBaseCollege(oldBaseCollege);
            baseCollege.setId(oldBaseCollege.getId());
        }

        // 添加统计详情数据
        insertStatisticsDetail(baseCollege);
    }

    private void insertStatisticsDetail(LabStatisticsBaseCollege baseCollege) {

        String type = baseCollege.getType();
        StatisticsTypeEnum statisticsTypeEnum = StatisticsTypeEnum.getByValue(type);

        List<LabOrgnization> labOrgnizations = orgnizationService.listLabByOrgId(
            baseCollege.getCollegeOrgId());

        switch (statisticsTypeEnum) {
            case Equipment:
                equipmentService.addEquipment(baseCollege, labOrgnizations);
                break;
            case Person:
                personService.addPerson(baseCollege, labOrgnizations);
                break;
            case Project:
                projectService.addProject(baseCollege, labOrgnizations);
                break;
            case Laboratory:
                laboratoryService.addLaboratory(baseCollege, labOrgnizations);
                break;
            default:
                throw new RuntimeException("找不到该统计类型");
        }
    }

    /**
     * 修改学院高基
     * 
     * @param labStatisticsBaseCollege 学院高基
     * @return 结果
     */
    @Override
    public int updateLabStatisticsBaseCollege(LabStatisticsBaseCollege labStatisticsBaseCollege)
    {
        labStatisticsBaseCollege.setUpdateTime(DateUtils.getNowDate());
        return labStatisticsBaseCollegeMapper.updateLabStatisticsBaseCollege(labStatisticsBaseCollege);
    }

    /**
     * 批量删除学院高基
     * 
     * @param ids 需要删除的学院高基ID
     * @return 结果
     */
    @Override
    public int deleteLabStatisticsBaseCollegeByIds(Long[] ids)
    {
        return labStatisticsBaseCollegeMapper.deleteLabStatisticsBaseCollegeByIds(ids);
    }

    /**
     * 删除学院高基信息
     * 
     * @param id 学院高基ID
     * @return 结果
     */
    @Override
    public int deleteLabStatisticsBaseCollegeById(Long id)
    {
        return labStatisticsBaseCollegeMapper.deleteLabStatisticsBaseCollegeById(id);
    }

    @Override
    public LabStatisticsBaseCollege selectLabStatisticsBaseCollegeDetailById(Long id) {
        LabStatisticsBaseCollege labStatisticsBaseCollege = this.selectLabStatisticsBaseCollegeById(id);
        String type = labStatisticsBaseCollege.getType();
        StatisticsTypeEnum statisticsTypeEnum = StatisticsTypeEnum.getByValue(type);

        List<? extends BaseEntity> detailList = null;

        switch (statisticsTypeEnum) {
            case Equipment:
                detailList = equipmentService.selectLabStatisticsEquipmentByBaseCollegeId(id);
                break;
            case Person:
                detailList = personService.selectLabStatisticsPersonByBaseCollegeId(id);
                break;
            case Project:
                detailList = projectService.selectLabStatisticsProjectByBaseCollegeId(id);
                break;
            case Laboratory:
                detailList = laboratoryService.selectLabStatisticsLaboratoryByBaseCollegeId(id);
                break;
            default:
                throw new RuntimeException("找不到该统计类型");
        }
        labStatisticsBaseCollege.setDetailList(detailList);

        return labStatisticsBaseCollege;
    }

    @Override
    public int passCheck(Long id) {
        LabStatisticsBaseCollege baseCollege = labStatisticsBaseCollegeMapper.selectLabStatisticsBaseCollegeById(
            id);
        baseCollege.setCheckStatus("1");
        return updateLabStatisticsBaseCollege(baseCollege);
    }

    @Override
    public int toSummary(Long id) {
        LabStatisticsBaseCollege baseCollege = labStatisticsBaseCollegeMapper.selectLabStatisticsBaseCollegeById(
            id);
        baseCollege.setStatus("1");
        return updateLabStatisticsBaseCollege(baseCollege);
    }
}
